package tv.youxing.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.entity.HomeModel;
import tv.youxing.android.utils.ShareUtils;

/**
 * Created by apple on 2017/4/19.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .adapter
 * 文件名：   HotAdapter
 * 创建者：   叶克武
 * 创建时间： 2017/4/19 下午1:07
 * 描述：    TODO
 */
public class HotAdapter extends BaseAdapter {


    private Context context;
    private List<HomeModel.ResultBean.PopularRecommenBean.ContListBeanXXX> hotModelList = new ArrayList<>();
    private LayoutInflater layoutInflater;

    public HotAdapter(Context context, List<HomeModel.ResultBean.PopularRecommenBean.ContListBeanXXX> hotModelList) {
        this.context = context;
        this.hotModelList = hotModelList;
        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        if (hotModelList != null && hotModelList.size() > 4) {
            return 4;
        } else if (hotModelList != null) {
            return hotModelList.size();
        } else {

            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        return hotModelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder==null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.layout_item_video, null);
            viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        int width = ShareUtils.getInt(context,"width",100);
//        PicassoUtils.loadImageViewSize(context, hotModelList.get(position).getCover(),(width-6)/2,(width-6)/2,R.drawable.logo,viewHolder.ivIcon);
        ImageLoader.getInstance().displayImage(hotModelList.get(position).getCover(),viewHolder.ivIcon);
        viewHolder.tvName.setText(hotModelList.get(position).getTitle());
        return convertView;
    }


    class ViewHolder{
        private ImageView ivIcon;
        private TextView tvName;
    }

}
